package com.ljh.webtask5.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author laijiahong
 */
@Controller
@RequestMapping("/api/v1/user/login")
public class LoginController {
    @Resource
    HttpServletRequest req;

    @GetMapping("")
    @Operation(summary = "返回登录视图")
    @ResponseStatus(HttpStatus.OK)
    public String loginForm() {
        return "Login";
    }

    @PostMapping("")
    @Operation(summary = "登录判断", description = "用户名密码一致跳转到图书列表，否则重新回到登录页面")
    public String login(@RequestParam String userName, @RequestParam String password) {
        if ("admin".equals(userName) && "123456".equals(password)) {
            HttpSession session = req.getSession();
            session.setAttribute("status", "success");
            return "redirect:/api/v1/book";
        } else {
            return "redirect:/api/v1/login";
        }
    }
}
